/*
 * TaskInstruction.java
 * 
 * last update: 16.01.2010 by Stefan Saru
 * 
 * author:	Power(power@kermit.cs.pub.ro)
 * 
 * Obs:
 */

package engine;

import java.util.*;

@SuppressWarnings("serial")
class TaskInstruction extends DefaultInstruction{

	/**
	 * The espressions of the actual parameters
	 */
	Vector paramExps;
	Vector paramInstances;
	DefaultInstruction theTask;
	TaskDescription  taskDescription;

	TaskInstruction(int delayBefore, NameSpace  ns, InstructionDescription desc,
			TaskDescription taskDescription, 
			Vector paramExps, Vector paramInstances,
			DefaultInstruction theTask, DefaultInstruction next){
		super(ns, delayBefore, desc);
		this.taskDescription = taskDescription;
		this.paramExps = paramExps;
		this.paramInstances = paramInstances;
		this.next = next;
		this.theTask = theTask;
		if (theTask != null)
			theTask.append(this);
	}

	public Instruction next() {
		switch(state) {
		case 0:
			state = 1;
			if (theTask != null)
				return theTask;
			return this;
		case 1:
			state = 0;
			return next;
		}
		throw new Error("illegal instructionstate");
	}

	public void execute()
	throws InterpretTimeException, SimulationStoppedException
	{
		switch (state) {
		case 0:
			waitToRun();
			xConsole.trace(this + " |->");
			taskDescription.storeParamValues(paramExps, paramInstances);
			break;
		case 1:
			taskDescription.fetchParamValues(paramExps, paramInstances);
			xConsole.trace(this + " <-|");
		}
	}

	public String toString(){
		return desc.toString();
	}
}



